package com.ipet.item.controller.downstage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ipet.ad.pojo.Ad;
import com.ipet.item.feign.AdFeignClient;
import com.ipet.item.pojo.SPU;
import com.ipet.item.service.CategoryService;
import com.ipet.item.service.SPUService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ipet")
public class IndexController {
    @Autowired
    private SPUService spuService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AdFeignClient adFeignClient;
    @GetMapping("/index")
    public String index(Model model , HttpServletRequest request){
//        获取标签
        List<Map<String, Object>> categoryTree = categoryService.findCategoryTree();
//        获取商品
        List<SPU> mainFood = spuService.list(new QueryWrapper<SPU>().like("title", "主粮"));
        List<SPU> snack = spuService.list(new QueryWrapper<SPU>().like("title", "零食"));
        List<SPU> health = spuService.list(new QueryWrapper<SPU>().like("title", "保健"));
        List<SPU> daily  = spuService.list(new QueryWrapper<SPU>().like("title", "日用"));
        List<SPU> toy  = spuService.list(new QueryWrapper<SPU>().like("title", "玩具"));
//        获取广告
        List<Ad> ads = adFeignClient.findAds();
        model.addAttribute("categoryTree" , categoryTree);
        model.addAttribute("mainFood" , mainFood);
        model.addAttribute("snack" , snack);
        model.addAttribute("health" , health);
        model.addAttribute("daily" , daily);
        model.addAttribute("toy" , toy);
        model.addAttribute("ads" , ads);
        return null;
    }
    @GetMapping("/spu/{id}")
    public String details(Model model ,@PathVariable Integer id){
        SPU spu = spuService.getById(id);
        model.addAttribute("spu" , spu);
        return "details";
    }
}
